<template>
  <div class="month-card">
    <a-form :model="formState" name="horizontal_login" layout="inline" autocomplete="off">
      <a-form-item label="车牌号码" name="carNumber">
        <a-input v-model:value="formState.carNumber" />
      </a-form-item>

      <a-form-item label="车主姓名" name="personName">
        <a-input v-model:value="formState.personName" />
      </a-form-item>

      <a-form-item label="状态" name="cardStatus">
        <a-select v-model:value="formState.cardStatus" :options="areas" />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" @click="search">查询</a-button>
      </a-form-item>
    </a-form>

    <div class="content-2">
      <div class="left">
        <a-button type="primary" @click="add">添加月卡</a-button>
        <a-button type="primary" @click="deleteAll">批量删除</a-button>
      </div>
      <div class="right">本园区共计 1530 个车位，月卡用户 0 人，车位占有率 0.00%</div>
    </div>
    <!-- :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" -->
    <a-table
      :pagination="pagination"
      @change="tableChange"
      @showSizeChange="showSizeChange"
      :dataSource="dataSource"
      :columns="columns"
      rowKey="id"
      :row-selection="rowSelection"
    >
      <template #bodyCell="{ record, column, index }">
        <div v-if="column.dataIndex === 'index'">{{ index + 1 }}</div>
        <template v-if="column.key === 'cardStatus'">
          <!-- {{ text == '0' ? '可用' : '已过期' }} -->
          {{ record.cardStatus == '0' ? '可用' : '已过期' }}
        </template>
        <template v-if="column.key === 'actions'">
          <!-- 模板上面不用在js声明，直接可以用的变量 $event  $router -->
          <a-button type="link" @click="$router.push(`/car/renewMonthCard/${record.id}`)"
            >续费</a-button
          >
          <a-button type="link" @click="$router.push(`/car/viewMonthCard/${record.id}`)"
            >查看</a-button
          >
          <a-button type="link" @click="$router.push(`/car/editMonthCard/${record.id}`)"
            >编辑</a-button
          >
          <a-button type="link" @click="deleteCard(record.id)">删除</a-button>
        </template>
      </template>
    </a-table>

    <!-- 确认弹出框 -->
    <a-modal
      v-model:visible="visible"
      title="Modal"
      ok-text="确认"
      cancel-text="取消"
      @ok="hideModal"
    >
      是否确认删除月卡
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';
import { useRouter } from 'vue-router';
import { getCardListReqeust, deleteParkingCardReqeust } from '@/apis/cart.ts';
import { message } from 'ant-design-vue';

const router = useRouter();
const formState = reactive<any>({
  page: 1,
  pageSize: 10,
  carNumber: '',
  personName: '',
  cardStatus: '',
});
const areas = [
  { label: '全部', value: '' },
  { label: '可用', value: '0' },
  { label: '已过期', value: '1' },
];

const dataSource = ref([]);
const columns = ref([
  {
    title: '序号',
    dataIndex: 'index',
    key: 'index',
  },
  {
    title: '车主名称',
    dataIndex: 'personName',
    key: 'personName',
  },
  {
    title: '联系方式',
    dataIndex: 'phoneNumber',
    key: 'phoneNumber',
  },
  {
    title: '车牌号码',
    dataIndex: 'carNumber',
    key: 'carNumber',
  },
  {
    title: '车辆品牌',
    dataIndex: 'carBrand',
    key: 'carBrand',
  },
  {
    title: '剩余有效天数',
    dataIndex: 'totalEffectiveDate',
    key: 'totalEffectiveDate',
  },
  {
    title: '状态',
    dataIndex: 'cardStatus',
    key: 'cardStatus',
  },
  {
    title: '操作',
    // 操作这一列不需要dataIndex，因为dataIndex是去渲染dataSource这个数组里面的元素的键的值的
    // dataIndex: 'cardStatus',
    key: 'actions',
  },
]);

const total = ref(0);
// 表格自带分页，不需要额外去找分页组件,
// 不是我们单独的分页组件，而是依附于表格自带的插件，自动集成了分页组件，但是没有文档
// react + ant-design
// vue3 + ant-design-vue
const pagination = computed(() => ({
  position: ['bottomRight'],
  total: total.value,
  current: formState.page,
  pageSize: formState.pageSize,
  showQuickJumper: true, // 是否显示跳转的结构
  showSizeChanger: true, // 显示每页显示条数切换器，下拉框
  pageSizeOptions: ['2', '3', '5', '10'],
  showTotal: (total: number, range: any) => `${range[0]}-${range[1]}  共${total} 条`,
}));

// table内置的分页插件，触发事件change
const tableChange = (pagination: any) => {
  formState.page = pagination.current;
  formState.pageSize = pagination.pageSize;
  search();
};
// 下面这个函数是我们从 分页插件的文档里面找出来的，看看呢不能用
const showSizeChange = () => {
  console.log('111111');
};

const search = async () => {
  const res = await getCardListReqeust({
    page: formState.page,
    pageSize: formState.pageSize,
    carNumber: formState.carNumber,
    personName: formState.personName,
    cardStatus: formState.cardStatus,
  });

  dataSource.value = res.data.rows;
  total.value = res.data.total;
};

search();

const add = () => {
  // 添加月卡的功能
  router.push('/car/addMonthCard');
};

const visible = ref<boolean>(false);
let currentId: any = null;

// 删除功能
const hideModal = async () => {
  // 点击了删除对话框的 确认按钮，执行的逻辑
  // 发请求
  const res: any = await deleteParkingCardReqeust(currentId);
  if (res.code === 10000) {
    //这个if是 删除请求成功了，刷新页面，关闭弹出框
    search();
    visible.value = false;
  }
};

const deleteCard = (id: any) => {
  // 打开对话框
  currentId = id;
  visible.value = true;
};

// 下面两个变量是针对于 勾选的
const selectedRowKeys = ref<any[]>([]);
// const onSelectChange = (selectedRowKeys: any[]) => {
//   console.log('selectedRowKeys changed: ', selectedRowKeys);
// };
const rowSelection = {
  onChange: (item: any[]) => {
    console.log('selectedRowKeys changed: ', item);
    selectedRowKeys.value = item;
  },
};
// 批量删除
const deleteAll = () => {
  if (selectedRowKeys.value.length === 0) {
    message.warning('请勾选需要删除的元素');
    return;
  }
  // 打开对话框
  currentId = selectedRowKeys.value.join();
  visible.value = true;
};
</script>

<style lang="scss">
.month-card {
  margin-top: 15px;
  padding: 15px;
  background-color: #fff;
  .ant-form {
    border-bottom: 1px solid #cccccc55;
    padding-bottom: 15px;
    .ant-form-item-control-input {
      width: 200px;
      .ant-input {
        border-radius: 5px;
      }
      .ant-select-selector {
        border-radius: 5px;
      }
    }
  }
  .content-2 {
    display: flex;
    justify-content: space-between;
    margin: 20px 0;
    .left {
      .ant-btn:first-child {
        margin-right: 10px;
      }
      .ant-btn {
        border-radius: 5px;
      }
    }
    .right {
      border: 1px solid rgb(145, 213, 255);
      background-color: rgba(145, 213, 255, 0.25);
      line-height: 32px;
      padding: 0 15px;
    }
  }
}
</style>
